package com.example.myapplication.module1.activity

import android.view.View
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityAppointmentVisitBinding
import com.example.myapplication.entity.AppointmentVisitBean
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module1.adapter.AppointmentVisitAdapter
import com.example.myapplication.module1.presenter.AppointmentVisitPresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.AfterSaleRequest
import com.example.myapplication.soaprequest.request.AppointmentVisitRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.UiTools
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import java.sql.Date
import java.util.*

/**
 * 预约拜访
 */

class AppointmentVisitActivity :
    BaseMvpActivity<AppointmentVisitActivity, AppointmentVisitPresenter, ActivityAppointmentVisitBinding>() {

    lateinit var loadPopup: LoadingPopupView
    private var pvTime: TimePickerView? = null
    private var curPage = 1
    private var dateType = ""
    private val appointmentVisitRequest = AppointmentVisitRequest()
    private val afterSaleRequest = AfterSaleRequest()
    private val soapRequestBody = SoapRequestBody()
    private val soapEnvelop = SoapEnvelop()
    private val tempAppointmentVisitBean = mutableListOf<AppointmentVisitBean>()
    private val calendar = Calendar.getInstance()
    private var params = ""
    private val appointmentVisitAdapter: AppointmentVisitAdapter by lazy {
        AppointmentVisitAdapter(this)
    }

    override fun getPresenter(): AppointmentVisitPresenter {
        return AppointmentVisitPresenter()
    }

    override fun getBinding(): ActivityAppointmentVisitBinding {
        return ActivityAppointmentVisitBinding.inflate(layoutInflater)
    }

    override fun initView() {
        loadPopup = builder.asLoading("加载中...")
        val itemInfo = bundleExtra?.getParcelable<ItemBean>("itemInfo")
        viewBinding.clToolbar.setTitle(itemInfo?.itemName)
        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {
            }
        }
        viewBinding.list.rvList.adapter = appointmentVisitAdapter

        //时间选择器
        pvTime = TimePickerBuilder(this) { date, _ ->
            when (dateType) {
                "1" -> {
                    viewBinding.tvFrom.text = UiTools.formatDate(date, "yyyy-MM-dd")
                }
                "2" -> {
                    viewBinding.tvTo.text = UiTools.formatDate(date, "yyyy-MM-dd")
                }
            }

        }
            .setType(booleanArrayOf(true, true, true, false, false, false))
            .build()

        viewBinding.tvFrom.setOnClickListener(this)
        viewBinding.tvTo.setOnClickListener(this)
        viewBinding.tvSearch.setOnClickListener(this)

        viewBinding.list.srlRefresh.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                curPage = 1
                getAppointmentVisit()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                curPage += 1
                getAppointmentVisit()
            }
        })

    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.tvFrom -> {
                dateType = "1"
                pvTime?.show()
            }
            R.id.tvTo -> {
                dateType = "2"
                pvTime?.show()
            }
            R.id.tvSearch -> {
                curPage = 1
                getAppointmentVisit()
            }
        }
    }


    override fun initData() {
        calendar.time = Date()
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        viewBinding.tvFrom.text = UiTools.formatDate(Date(calendar.timeInMillis), "yyyy-MM-dd")
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
        viewBinding.tvTo.text = UiTools.formatDate(Date(calendar.timeInMillis), "yyyy-MM-dd")
        params = bundleExtra?.getString("params", "").toString()
        getAppointmentVisit()
    }

    /**
     * 获取预约拜访列表
     */
    private fun getAppointmentVisit() {
        if (curPage == 1) {
            tempAppointmentVisitBean.clear()
        }
        when (params) {
            "jhrwyybf" -> {
                // 预约拜访
                appointmentVisitRequest.qsrq = viewBinding.tvFrom.text.toString()
                appointmentVisitRequest.zzrq = viewBinding.tvTo.text.toString()
                appointmentVisitRequest.curpage = "$curPage"
                if (viewBinding.rbNoVisit.isChecked) {
                    appointmentVisitRequest.zt = "0"
                }
                if (viewBinding.rbYesVisit.isChecked) {
                    appointmentVisitRequest.zt = "1"
                }
                soapRequestBody.value = appointmentVisitRequest
            }
            "jhrwshhf" -> {
                // 售后回访
                afterSaleRequest.qsrq = viewBinding.tvFrom.text.toString()
                afterSaleRequest.zzrq = viewBinding.tvTo.text.toString()
                afterSaleRequest.curpage = "$curPage"
                if (viewBinding.rbNoVisit.isChecked) {
                    afterSaleRequest.zt = "0"
                }
                if (viewBinding.rbYesVisit.isChecked) {
                    afterSaleRequest.zt = "1"
                }
                soapRequestBody.value = afterSaleRequest
            }
        }
        soapEnvelop.body = soapRequestBody
        mPresenter?.getAppointmentVisit(soapEnvelop)
    }

    fun showLoad() {
        if (!viewBinding.list.srlRefresh.isRefreshing && !viewBinding.list.srlRefresh.isLoading) {
            loadPopup.show()
        }
    }

    fun dismissLoad() {
        loadPopup.dismiss()
        if (viewBinding.list.srlRefresh.isRefreshing || viewBinding.list.srlRefresh.isLoading) {
            viewBinding.list.srlRefresh.finishRefresh()
            viewBinding.list.srlRefresh.finishLoadMore()
        }
    }

    fun setAppointmentVisit(result: String) {
        val appointmentVisitBeanList = GsonUtil.fromJson<MutableList<AppointmentVisitBean>>(
            result,
            object : TypeToken<MutableList<AppointmentVisitBean>>() {}.type
        )
        if (appointmentVisitBeanList.isNullOrEmpty()) {
            UiTools.showToast(R.string.noMoreInfo)
            viewBinding.list.srlRefresh.setEnableLoadMore(false)
        } else {
            tempAppointmentVisitBean.addAll(appointmentVisitBeanList)
        }
        appointmentVisitAdapter.appointmentVisitBeanList = tempAppointmentVisitBean
        if (tempAppointmentVisitBean.isNotEmpty()) {
            viewBinding.noData.clNoData.visibility = View.GONE
        } else {
            viewBinding.noData.clNoData.visibility = View.VISIBLE
        }
    }

}
